package com.springboot.middleware.middleware_rabbitmq_logforlogin.server.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class LoginRabbitMQConfig {
    private static final String exchangeName = "dev.login.topic.exchange";
    private static final String queueName = "dev.login.queue";
    private static final String routingKey = "dev.login.log";

    @Bean
    public Queue loginQueue() {
        return new Queue(queueName, true);
    }

    @Bean
    public TopicExchange loginExchange() {
        return new TopicExchange(exchangeName, true, false);
    }

    @Bean
    public Binding loginQueueBinding() {
        return BindingBuilder.bind(loginQueue()).to(loginExchange()).with(routingKey);
    }


    /**
     * 解决方法:添加这个类进行序列化解析
     * 会自动识别
     * @param objectMapper json序列化实现类
     * @return mq 消息序列化工具
     */
    @Bean
    public MessageConverter jsonMessageConverter(ObjectMapper objectMapper) {
        return new Jackson2JsonMessageConverter(objectMapper);
    }
}
